<?php
/*
 * @Author: CHINAKAIYUAN<sunkaiyuan@namenode.cn>
 * @Date: 2022-02-03 00:33:05
 * @LastEditTime: 2022-08-25 21:33:51
 * @LastEditors: CHINAKAIYUAN
 * @Description: 
 */

namespace App\Models;

use App\Repositories\FinanceVoucherRepository;
use App\Traits\CompanyTrait;
use App\Traits\VoucherTrait;

class FinanceAcceptance extends BaseModel
    {
    use CompanyTrait;
    use VoucherTrait;
    public $timestamps = true;
    public $table = "sys_finance_acceptance";
    protected $guarded = [];
    // 
    public function status()
        {
        return $this->belongsTo(Dic::class, "status_id", "key")->where(function ($q)
            {
            $q->where("code", 'AcceptanceStatus');
            });
        }

    public function schedule()
        {
        return $this->belongsTo(Dic::class, "schedule_id", "key")->where(function ($q)
            {
            $q->where("code", 'AcceptanceSchedule');
            });
        }
    public function billtrade()
        {
        return $this->belongsTo(CompanyBilltrade::class);
        }
    public function finance($model = null, $voucher = null)
        {
        return;
        }
    public function handVoucher()
        {
        return $this->belongsTo(FinanceVoucher::class, 'handvoucher_id');
        }
    public function handFinace()
        {
        if ($this->status_id != 0) {
            fail("承兑【{$this->status->name}】,持有失败！");
            }
        $summry                 = "应收承兑{$this->code}{$this->sourceremark}";
        $data                   = $this->toArray();
        $data['billdate']       = $this->recedate;
        $data['company_id']     = $this->customer_id;
        $data['tax']            = 1;
        $data['invoice_amount'] = 0;

        $details[] = FinanceVoucherRepository::mergeVoucher(100294, $summry, $this->amount, 0, $data);
        $details[] = FinanceVoucherRepository::mergeVoucher(1122, $summry, 0, $this->amount - $this->buyinterest, $data);
        if ($this->buyinterest > 0) {
            $summry    = "应收承兑{$this->code}-贴息";
            $details[] = FinanceVoucherRepository::mergeVoucher(560302, $summry, 0, $this->buyinterest, $data);
            }
        $voucher              = FinanceVoucherRepository::CreateVoucher($this->recedate, $this->trade_id, $details, $this, $this->handVoucher?->id);
        $this->handvoucher_id = $voucher->id;
        auditSave($this);
        return FinanceVoucherRepository::auditVoucher($voucher);
        }
    public function useVoucher()
        {
        return $this->belongsTo(FinanceVoucher::class, 'usevoucher_id');
        }
    public function useFinace()
        {
        if ($this->status_id != 1) {
            fail("承兑【{$this->status->name}】,使用失败！");
            }
        $summry             = "应付承兑{$this->code}{$this->handleremark}";
        $data               = $this->toArray();
        $data['billdate']   = $this->usedate;
        $data['company_id'] = $this->supplier_id;
        $data['tax']        = 1;
        $details[]          = FinanceVoucherRepository::mergeVoucher(100294, $summry, 0, $this->amount, $data);
        $details[]          = FinanceVoucherRepository::mergeVoucher(2202, $summry, $this->amount - $this->saleinterest, 0, $data);
        if ($this->saleinterest > 0) {
            $summry    = "应付承兑{$this->code}-贴息";
            $details[] = FinanceVoucherRepository::mergeVoucher(560302, $summry, $this->saleinterest, 0, $data);
            }
        $voucher             = FinanceVoucherRepository::CreateVoucher($this->usedate, $this->trade_id, $details, $this, $this->useVoucher?->id);
        $this->usevoucher_id = $voucher->id;
        auditSave($this, true, true, 2);
        return FinanceVoucherRepository::auditVoucher($voucher);
        }
    public function debtVoucher()
        {
        return $this->belongsTo(FinanceVoucher::class, 'debtvoucher_id');
        }
    public function debtFinace()
        {
        if ($this->status_id != 1) {
            fail("承兑【{$this->status->name}】,入账失败！");
            }
        $summry               = "承兑入账{$this->code}{$this->handleremark}";
        $data                 = $this->toArray();
        $data['billdate']     = $this->recedate;
        $data['company_id']   = 0;
        $data['tax']          = 1;
        $details[]            = FinanceVoucherRepository::mergeVoucher(100294, $summry, 0, $this->amount, $data);
        $details[]            = FinanceVoucherRepository::mergeVoucher($this->account_id, $summry, $this->amount, 0, $data);
        $voucher              = FinanceVoucherRepository::CreateVoucher($this->recedate, $this->trade_id, $details, $this, $this->debtVoucher?->id);
        $this->debtvoucher_id = $voucher->id;
        auditSave($this, true, true, 3);
        return FinanceVoucherRepository::auditVoucher($voucher);
        }

    public function nofinance($orders = null, $voucher = null)
        {
        $v              = $orders ?? $this;
        $handvoucher_id = $v->handvoucher_id;
        $usevoucher_id  = $v->usevoucher_id;
        $debtvoucher_id = $v->debtvoucher_id;
        if ($voucher) {
            if ($voucher->id == $handvoucher_id) {
                if ($usevoucher_id > 0) {
                    fail("承兑已使用！不能作废【持有】时产生的凭证，必须先作废使用凭证");
                    }
                if ($debtvoucher_id > 0) {
                    fail("承兑已到期入账！不能作废【持有】时产生的凭证，必须先作废入账凭证");
                    }
                $v->handvoucher_id = 0;
                auditSave($v, false);
                }
            else {
                if ($voucher->id == $usevoucher_id) {
                    $v->usevoucher_id = 0;
                    }
                else {
                    $v->debtvoucher_id = 0;
                    }
                auditSave($v, false, true, 1);
                }
            }
        return $v;
        }
    }
